
package projetjava;

import Composite.Element;
import java.util.*;
import javax.swing.*;

public class AdaptateurModelListeVirtuelle extends AbstractListModel implements Observer {
   
    private Element model;
    private ArrayList maListe;
    
    public AdaptateurModelListeVirtuelle() {
       
        this.maListe = new ArrayList();
        
    }

    public int getSize() {
        return maListe.size();
    }

    public Object getElementAt(int index) {
        return this.maListe.get(index);
    }

    public void update(Observable o, Object arg) {
        if(maListe.size()<model.getChildCount()){
            this.ajouterElement(arg);
            ThreadVignette monThread = new ThreadVignette(this);
            monThread.start();

        }
        else  if(maListe.size()>model.getChildCount()){
            this.supprimerElement(arg);
             }
        else{
            
            this.mettreAjourStructure((Element)arg);
        }
    }
    
    public Element getModel(){
        
        return this.model;
    }
    
    public ArrayList getElements(){
        
        return this.maListe;
    }
    
    public void mettreAjourStructure(Element e){
        
        int index = this.maListe.indexOf(e);
        this.fireContentsChanged(this,index,index);
    }
    
    public void supprimerElement (Object e){
        int index = this.maListe.indexOf(e);
        maListe.remove(e);
        this.fireIntervalRemoved(this,index,index);
    }
    
    public void ajouterElement (Object e){
        maListe.add(e);
        this.fireIntervalAdded(this,this.getSize(),this.getSize());
    }
    
    public void setModel(Element nvoModel){
        if(model!=null){
        model.deleteObserver(this);
        }
        int index=this.maListe.size();
        this.maListe.clear();
        this.fireIntervalRemoved(this,0,index);
        
        this.model=nvoModel;
        model.addObserver(this);
        Iterator it = (new ArrayList(this.model.getChildren())).iterator();
        while(it.hasNext()){
            
            Element suiv = (Element)it.next();
            this.ajouterElement(suiv);
        }
        
        ThreadVignette monThread = new ThreadVignette(this);
        monThread.start();
    }
}
